Java 上手 TypeScript快速路线
Java 上手 TypeScript快速路线
语言风格
和Java一样。句末可以加;
可不加。有些极端情况要加,因此把握不住就都加;
,把握得住可以不加。
数据类型
TypeScript: Documentation - Everyday Types (typescriptlang.org)
TypeScript: Documentation - Object Types (typescriptlang.org)
基本类型
类型 | 字面量 | ||
---|---|---|---|
number | 123 | 在JavaScript中,全部数都是浮点数 | 123 (限定为123这个数字) |
boolean | true false | 默认false是0,但是请不要这么用 | |
string | "hello" 'world' | 在JavaScript中,string也是基本类型! | "hello" (限定为'hello'这个字符串) |
undefined | undefined | 变量已经声明没有初始化, 对象不存在该属性, 函数没有返回值, 函数参数没有传入。 在条件语句中当作false |
引用类型
类型 | 字面量 | 其他方式 | |
---|---|---|---|
number[] | [1,2,3,4] | 数组,任何类型都可以加个[],当作数组类型 | Array<number> |
object | 对象,或者说是键值对的映射 | ||
null | null | 空指针(空数组是[],空对象是{}) 有值,但是未定义用undefined;无值,用null。 在条件语句中当作false | |
/find/g | 正则表达式,建其他教程 |
其他类型
类型 | |||
---|---|---|---|
作为所有类型的基类 | any | ||
unkown | |||
never | |||
函数的类型 | (a:number)=>void | ||
索引类型,在对象中,表示这个对象的索引键名是string,值是string。 TypeScript: Documentation - Mapped Types (typescriptlang.org) | [a:string]:string |
文件的组织方式
模块其他内容参见:TypeScript: Documentation - Modules (typescriptlang.org)
import {func1} from './AFile.js'
export function func2(){
func1();
}
import最好使用相对路径。
在文件中被export的内容可以被其他文件import。
export相当于文件层级的public。(也就是Java中包访问权限修饰符:public class这个public)
量声明
let
和const
用于声明变量和常量
const a = 123
const a:number = 123 //带有类型注释的常量声明
let a = 123
let a:number = 123
let
声明可变量
const
声明不可变量,其实就是不可赋值量,相当于Java中的 final
。因此,对于引用类型来说,内部存储的内容依然是可变的,例如:
const list = [1,2,3,4,5]
list[0] = 4
// 4,2,3,4,5
list = [3,2,3,4,5] //error,不可以再次赋值
const test = {a:3,b:4}
test["a"] = 5 //对象的两种访问方式
test.b = 6
//test = {a:5,b:6}
readonly
可以写在类型前面,用于标记本对象也是不可更改的。
let b: readonly number[] = [1, 2, 3];
b[0] = 101; // error
TypeScript: Documentation - TypeScript for Functional Programmers (typescriptlang.org)
对象和数组的访问
[]
是根据索引取内容操作符,对于对象来说,索引是字符串;但对于数组来说,索引是数字。
对象也可以用数字来当索引。
const list = [1,2,3,4,5]
list[0] = 4
const test = {a:3,b:4}
test["a"] = 5
函数声明
普通方式
function test( a:number, b:number ):number {
return a+b
}
lambda方式
const test = (a,b)=> a+b //lambda表达式
const test = (a:number,b:number):number => { //直接在lambda表达式上标注类型
return a+b
}
const test:(a:number,b:number)=>number=(a,b)=>{ //在声明的量上标注类型
return a+b
}
//高阶函数
const test = (a:number)=>(b:number)=>{
} //test(a)返回函数,test(a)(b)返回结果
泛型
function test<T>(arg:T){
}
function test(arg:any){
}
面向对象内容
类声明
export class Test extends TestFather implements ITest {
constructor(){
//构造函数
}
public name:string = "" //必须先赋值
public getName(from:string):string{
this.name = from //必须用this
return from
}
static common:string = "" //从属于类,和Java一样
}
泛型一样在类名后面使用
类使用
import {Test} from './test.js'
const test1 = new Test() //调用了构造函数
接口声明
interface ITest{
a:string;
b:string;
readonly c:string;
}
对于类和接口来说,readonly标记的属性不可再次赋值。
用在逻辑上的关键字和运算符
as
类型假定,可以当作类型转换使用
const a = "hello" as string
instanceof
判断从属于的类型
if (x instanceof Test){
//在这里x就是Test类型的,不用再强制类型转换
}
typeof
返回量的类型,返回值可以当作字符串(值),可以当作类型
TypeScript: Documentation - Typeof Type Operator (typescriptlang.org)
switch (typeof "hello"){
case "string":
} //当作字符串
const a: typeof "hello" = "world" //当作类型用
in
、of
in用于判断键名是否在对象/类型中
const animal = { swim:"test"}
if ("swim" in animal){
}
in取键名,of取键值
const obj = {a:1 , b:2}
for( const i in obj){
//i是obj的键名,也就a、b两个字符串
}
const list = [1,2,3]
for( const j of list){
//j是list的键值,也就是1、2、3
//这里用for in则拿到的是0、1、2
}
keyof
取出对象的键,返回值当作类型
const obj = { a:1 , b:2 }
let key:keyof obj
for(key in obj){
obj[key] = 2
}
==
和===
===
要求类型和值都要移植。一般用===
|
和&
类型运算联合类型|
:两者选其中一个
交叉类型&
:两者都要
let a:string|number //a是string类型或者number类型
let b:"hello"|"world" //b只能是"hello"或者"world"其中一个
let c:{a:string} & {b:string} //c:{a:string,b:string}
type
TypeScript: Documentation - Creating Types from Types (typescriptlang.org)
类型别名,将类型赋值给它。和interface类型,区别在于type=后面可以进行类型运算
type b = { test:string }
?
!
function liveDangerously(x?: number | null) { //加个问号表示可以不存在,也就是undefined
//x?.toFixed()表示当x有toFixed这个方法的时候继续执行,没有的话也不会报错
console.log(x!.toFixed()); //!表示认为x是非空的
}
其他细节
...
展开运算符
const test1 = {b:string,c:string}
const test2 = {d:string,a:string}
const test3 = { ...test1, test:test2}
/*
test3 = {
b:string,c:string,
test:{d:string,a:string}
}*/
const list1 = [1,2,3,4]
const list2 = [...list1,5,4]
//list2 = [1,2,3,4,5,4]
模版字符串
用反引号标注,插入的内容用${}
标注。
const test = 1
const test2 = `hello from ${test}`
//test2 = 'hello from 1'
函数用对象的类型来表示类型
type funcType = {(from:string):void}
const test:funcType = (from)=>{
}